(function ($) {
    window.admin = {};
    admin.utils = {};
    admin.editor = {};
    admin.article = {};
    admin.category = {};
    admin.comment = {};
    admin.tag = {};
    admin.link = {};
    admin.user = {};
    admin.daily = {};

    admin.editor.html = function (id) {
        return KindEditor.create('#' + id, {
            cssPath: [
                window.variables.base + '/static/js/kindeditor/plugins/code/prettify.css'
            ]
        });
    };

    admin.editor.markdown = function (id) {
        return editormd({
            id: id,
            width: "100%",
            height: "480px",
            toolbarIcons: function () {
                return [
                    "undo", "redo", "|",
                    "bold", "del", "italic", "quote", "ucwords", "uppercase", "lowercase", "|",
                    "h1", "h2", "h3", "h4", "h5", "h6", "|",
                    "list-ul", "list-ol", "hr", "|",
                    "link", "reference-link", "image", "code", "preformatted-text", "code-block", "table", "datetime", "emoji", "html-entities", "pagebreak", "|",
                    "goto-line", "watch", "preview", "fullscreen"
                ]
            },
            syncScrolling: "single",
            path: window.variables.base + "/static/js/editor.md/lib/",
            placeholder: '',
            emoji: true
        });
    };

    ////////////////////////////////////////////////////////////
    //admin.utils
    ////////////////////////////////////////////////////////////
    admin.utils.showLoading = function () {
        $('<div class="datagrid-mask" style="z-index:1000"></div>').css({
            display: 'block',
            width: '100%',
            height: $(window).height()
        }).appendTo($(window.top.document.body));
        $('<div class="datagrid-mask-msg"></div>').html('加载中，请稍后...').appendTo($(window.top.document.body)).css({
            display: 'block',
            left: ($(document.body).outerWidth(true) - 190) / 2,
            top: ($(window).height() - 45) / 2
        });
    };

    admin.utils.hideLoading = function () {
        $('.datagrid-mask', window.top.document).remove();
        $('.datagrid-mask-msg', window.top.document).remove();
    };

    ////////////////////////////////////////////////////////////
    //admin.article
    ////////////////////////////////////////////////////////////

    /**
     * 删除文章
     * @param id
     */
    admin.article.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/article/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#articleForm').submit();
                }
            });
        }
    };


    /**
     * 文章分类表单验证
     */
    admin.article.validateForm = function () {
        $('#articleForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                title: {
                    required: true,
                    maxlength: 50
                },
                category: {
                    required: true
                },
                content: {
                    required: true
                }
            },
            messages: {
                title: {
                    required: '请填写文章标题',
                    maxlength: '文章标题最多不能超过{0}个字'
                },
                category: {
                    required: '请选择文章分类'
                },
                content: {
                    required: '文章内容不能为空'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.category
    ////////////////////////////////////////////////////////////

    /**
     * 删除分类
     *
     * @param id
     */
    admin.category.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/category/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#categoryForm').submit();
                }
            });
        }
    };


    /**
     * 文章分类表单验证
     */
    admin.category.validateForm = function () {
        $('#categoryForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                name: {
                    required: true,
                    maxlength: 12
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: '请填写分类名称',
                    maxlength: '分类名称最多不能超过{0}个字'
                },
                description: {
                    maxlength: '描述最多不能超过{0}个字'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.comment
    ////////////////////////////////////////////////////////////

    /**
     * 删除评论
     *
     * @param id
     */
    admin.comment.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/comment/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#commentForm').submit();
                }
            });
        }
    };


    /**
     * 标记为垃圾
     *
     * @param id
     */
    admin.comment.spam = function (id) {
        if (confirm("确认讲该数据标记为垃圾数据吗？")) {
            httpPost('/admin/comment/spam/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#commentForm').submit();
                }
            });
        }
    };

    /**
     * 标记为垃圾
     *
     * @param id
     */
    admin.comment.recover = function (id) {
        httpPost('/admin/comment/recover/do', {'id': id}, function (data) {
            if (data == true) {
                $('#commentForm').submit();
            }
        });
    };


    ////////////////////////////////////////////////////////////
    //admin.tag
    ////////////////////////////////////////////////////////////

    /**
     * 删除标签
     *
     * @param id
     */
    admin.tag.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/tag/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#tagForm').submit();
                }
            });
        }
    };


    /**
     * 标签表单验证
     */
    admin.tag.validateForm = function () {
        $('#tagForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                name: {
                    required: true,
                    maxlength: 12
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: '请填写标签名称',
                    maxlength: '标签名称最多不能超过{0}个字'
                },
                description: {
                    maxlength: '描述最多不能超过{0}个字'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.link
    ////////////////////////////////////////////////////////////

    /**
     * 删除链接
     *
     * @param id
     */
    admin.link.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/link/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#linkForm').submit();
                }
            });
        }
    };


    /**
     * 链接表单验证
     */
    admin.link.validateForm = function () {
        $('#linkForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                name: {
                    required: true,
                    maxlength: 32
                },
                url: {
                    required: true,
                    maxlength: 1024,
                    url: true
                },
                description: {
                    maxlength: 500
                }
            },
            messages: {
                name: {
                    required: '请填写链接名称',
                    maxlength: '链接名称最多不能超过{0}个字'
                },
                url: {
                    required: '请填写链接地址',
                    maxlength: '链接地址最多不能超过{0}个字',
                    url: "链接地址必须是一个合法的URL"
                },
                description: {
                    maxlength: '描述最多不能超过{0}个字'
                }
            }
        });
    }


    ////////////////////////////////////////////////////////////
    //admin.user
    ////////////////////////////////////////////////////////////
    admin.user.validateForm = function () {
        $('#userForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                nickname: {
                    required: true,
                    maxlength: 32
                },
                username: {
                    required: true,
                    maxlength: 32
                },
                email: {
                    required: true,
                    maxlength: 128,
                    email: true
                }
            },
            messages: {
                nickname: {
                    required: '请填写昵称',
                    maxlength: '昵称最多不超过{0}个字'
                },
                username: {
                    required: '请填写用户名',
                    maxlength: '用户名最多不超过{0}个字',
                },
                email: {
                    required: '请填写邮箱',
                    maxlength: '邮箱最多不能超过{0}个字',
                    email: '邮箱格式不合法'
                }
            }
        });
    }

    admin.user.validatePwdForm = function () {
        $('#passwordForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                oldPassword: {
                    required: true
                },
                newPassword: {
                    required: true
                },
                rePassword: {
                    required: true,
                    equalTo: '#newPassword'
                }
            },
            messages: {
                oldPassword: {
                    required: '请验证旧密码'
                },
                newPassword: {
                    required: '请填写新密码'
                },
                rePassword: {
                    required: '请确认新密码',
                    equalTo: '两次输入密码必须相同'
                }
            }
        });
    }

    ////////////////////////////////////////////////////////////
    //admin.daily
    ////////////////////////////////////////////////////////////
    /**
     * 删除文章
     * @param id
     */
    admin.daily.delete = function (id) {
        if (confirm("确认删除该条数据吗？")) {
            httpPost('/admin/daily/delete/do', {'id': id}, function (data) {
                if (data == true) {
                    $('#dailyForm').submit();
                }
            });
        }
    };
    admin.daily.validateForm = function () {
        $('#dailyForm').validate({
            wrapper: 'li',
            errorLabelContainer: '#error',
            rules: {
                title: {
                    required: false,
                    maxlength: 50
                },
                content: {
                    required: true
                }
            },
            messages: {
                title: {
                    maxlength: '日记标题最多不能超过{0}个字'
                },
                content: {
                    required: '日记内容不能为空'
                }
            }
        });
    }
})(jQuery);
